package cn.tedu.hotel.serve.controller;

import cn.tedu.hotel.serve.pojo.dto.*;
import cn.tedu.hotel.serve.pojo.entity.User;
import cn.tedu.hotel.serve.pojo.vo.AdminUserVO;
import cn.tedu.hotel.serve.pojo.vo.UserStandardVO;
import cn.tedu.hotel.serve.pojo.vo.UserVO;
import cn.tedu.hotel.serve.security.UserForDetails;
import cn.tedu.hotel.serve.security.UserLoginPrincipal;
import cn.tedu.hotel.serve.service.IUserService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
@Api(tags = "01. 用户管理模块")
@Validated
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping("/add-new")
    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Validated UserRegisterDTO userRegisterDTO) {
        log.debug("开始处理【注册用户】的请求，参数：{}", userRegisterDTO);
        userService.addNew(userRegisterDTO);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 150)
    public JsonResult<String> login(@Validated UserLoginInfoDTO userLoginInfoDTO) {
        log.debug("开始处理【登录用户】的请求，参数：{}", userLoginInfoDTO);
        String jwt = userService.Login(userLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    //TODO 测试用的方法记得删除
    @GetMapping("/count")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperation("测试用户数量")
    @ApiOperationSupport(order = 10)
    public JsonResult<Integer> count(@ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal) {
        log.debug("开始处理【测试计算数量】的请求");
        log.debug("开始处理【查询用户列表】的请求，参数：无");
        log.debug("当事人信息：{}", userLoginPrincipal);
        log.debug("当事人信息中的ID：{}", userLoginPrincipal.getId());
        log.debug("当事人信息中的用户名：{}", userLoginPrincipal.getUsername());
        log.debug("当事人信息中的昵称：{}", userLoginPrincipal.getNickname());
        int count = userService.count();
        return JsonResult.ok(count);
    }

    @GetMapping("/nickname")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperation("返回用户昵称")
    @ApiOperationSupport(order = 200)
    public JsonResult<String> nickname(@ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal) {
        log.debug("开始处理【返回用户昵称】的请求");
        return JsonResult.ok(userLoginPrincipal.getNickname());
    }

    @PostMapping("/user/updateInfoById")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperation("用户修改个人信息")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal,
            @Validated UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【用户修改个人信息】的请求，参数：{}", userUpdateDTO);
        userService.updateInfoById(userLoginPrincipal.getId(), userUpdateDTO);
        return JsonResult.ok();
    }

    @PostMapping("/user/updateUserPassword")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperation("用户修改密码")
    @ApiOperationSupport(order = 301)
    public JsonResult<Void> updateUserPassword(
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal,
            @Validated UserUpdatePasswordDTO userUpdatePasswordDTO) {
        log.debug("开始处理【用户修改个人密码】的请求，参数：{}", userUpdatePasswordDTO);
        userService.updateUserPassword(userLoginPrincipal.getId(),userUpdatePasswordDTO);
        return JsonResult.ok();
    }

    @GetMapping("/user")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperation("用户查看个人信息")
    @ApiOperationSupport(order = 400)
    public JsonResult<UserStandardVO> getStandardById(@ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal) {
        Long id = userLoginPrincipal.getId();
        UserStandardVO queryResult = userService.getStandardById(id);
        log.debug("UserController根据id查找结果:{}", queryResult);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/adminLogin")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 900)
    public JsonResult<String> adminLogin(@Validated UserLoginInfoDTO userLoginInfoDTO) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", userLoginInfoDTO);
        String jwt = userService.Login(userLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/admin/add-new")
    @ApiOperation("添加用户")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 500)
    public JsonResult<Void> adminAddNew(@Validated UserRegisterDTO userRegisterDTO) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userRegisterDTO);
        userService.addNew(userRegisterDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('adminRoot')")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('adminRoot')")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/delete")
    @ApiOperation("删除用户(单个)")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 600)
    public JsonResult<Void> deleteAdminUser(@PathVariable Long id) {
        log.debug("开始处理[查看用户(单个)的请求");
        userService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/update")
    @ApiOperation("管理员修改用户")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 700)
    public JsonResult<Void> adminUpdateUser(@PathVariable Long id, AdminUserDTO adminUserDTO) {
        log.debug("开始处理【管理员修改用户】的请求，参数：{},{}", id,adminUserDTO);
        userService.adminUpdateUser(id,adminUserDTO);
        return JsonResult.ok();
    }



    @GetMapping("/admin/getUserList")
    @ApiOperation("查看用户列表")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<AdminUserVO>> getUserList() {
        log.debug("开始处理[查看用户列表]的请求");
        List<AdminUserVO> adminUserVOList = userService.getAdminUserVOList();
        return JsonResult.ok(adminUserVOList);
    }

    @GetMapping("/admin/{id:[0-9]+}/getAdminUser")
    @ApiOperation("查看用户(单个)")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 801)
    public JsonResult<AdminUserVO> getAdminUserVO(@PathVariable Long id) {
        log.debug("开始处理[查看用户(单个)的请求");
        AdminUserVO adminUserVO = userService.getAdminUserVOById(id);
        return JsonResult.ok(adminUserVO);
    }

    //TODO 退出登录功能，只有前端实现了，后端没有实现
}
